package main

import (
	//"bufio"
	"bytes"
	"code.google.com/p/go.text/encoding/simplifiedchinese"
	"code.google.com/p/go.text/transform"
	//"flag"
	"fmt"
	goq "github.com/PuerkitoBio/goquery"
	"io/ioutil"
	"net"
	"net/http"
	"os"
	//"runtime"
	"strconv"
	"strings"
	"sync"
	"time"
)

var (
	main_domain = "" //主域名
	waitgroup   = &sync.WaitGroup{}
	LOG         = "centanet"
)

func main() {
	var citycode = "" //城市编号
	if len(os.Args) < 2 {
		citycode = "bj"
	} else {
		citycode = os.Args[1]
	}
	switch citycode {
	default:
		{
			main_domain = "http://bj.centanet.com"
			LOG = "bj." + LOG
			break
		}
	case "bj":
		{
			main_domain = "http://bj.centanet.com"
			LOG = "bj." + LOG
			break
		}
	}
	tmpurl := fmt.Sprintf("%s/jingjiren/", main_domain)
	doc, e := getDoc(tmpurl)
	if e != nil {
		fmt.Println(e.Error())
		os.Exit(-1)
	}

	//有多少分页
	str_recordCount := filterStr(doc.Find(".fyahei em").Text())
	recordCount, e := strconv.Atoi(str_recordCount)
	if e != nil {
		fmt.Println(e.Error())
		os.Exit(-1)
	}
	pageSize := 10
	totalCount := recordCount / pageSize
	if recordCount%pageSize > 0 {
		totalCount++
	}

	for curPage := 1; curPage <= totalCount; curPage += 5 {
		for j := curPage; j < curPage+5 && j <= totalCount; j++ {
			tmpurl := fmt.Sprintf("%s/jingjiren/g%d/", main_domain, j)
			waitgroup.Add(1)
			go getAgentlist(tmpurl)
		}
		waitgroup.Wait()
	}

	fmt.Println("done.")
}

//经纪人信息
func getAgentlist(url string) {
	defer waitgroup.Done()
	doc, e := getDoc(url)
	if e != nil {
		return
	}
	doc.Find(".broker_list li").Each(func(i int, sec *goq.Selection) {
		var (
			fenhang string = "" //分行
			address string = "" //地址
			xiaoqu  string = "" //主营小区
			name    string = "" //经纪人
			//phone   string = "" //座机
			mobile  string = "" //手机
			//service_year int    = 0  //中原服务年限
			//chengjiao    int    = 0  //近两年共成交
			esf_num  int = 0 //二手房套数
			rent_num int = 0 //租房套数
		)
		fenhang = filterStr(sec.Find(".js_map_animate").Text())
		if "" == fenhang {
			fenhang = "--"
		}
		address = filterStr(sec.Find(".address").Text())
		if "" == address {
			address = "--"
		}
		xiaoqu_arr := sec.Find(".xi").Children().Map(func(i int, sec *goq.Selection) string {
			return sec.Text()
		})
		xiaoqu = strings.Join(xiaoqu_arr, ",")
		if "" == xiaoqu {
			xiaoqu = "--"
		}
		name = filterStr(sec.Find(".name").Text())
		if "" == name {
			name = "--"
		}
		//phone = filterStr(sec.Find(".js_agent400").Text())
		if vv, ok := sec.Find(".js_agent400").Attr("zvalue"); ok {
			tmp_mobile := strings.Split(vv, "'")
			if len(tmp_mobile) > 1 {
				mobile = tmp_mobile[1]
			}
		}
		if "" == mobile {
			mobile = "--"
		}
		/*
			tmpyear := sec.Find(".info").Find("span").Map(func(i int, sec *goq.Selection) string {
				return sec.Text()
			})
			if len(tmpyear) > 1 {
				service_year, _ = strconv.Atoi(tmpyear[0])
				chengjiao, _ = strconv.Atoi(tmpyear[1])
			} else if len(tmpyear) == 1 {
				service_year, _ = strconv.Atoi(tmpyear[0])
			}
		*/
		tmpnum := sec.Find(".outstanding").Find("a").Map(func(i int, sec *goq.Selection) string {
			return strings.Replace(sec.Text(), "套", "", -1)
		})
		if len(tmpnum) > 1 {
			esf_num, _ = strconv.Atoi(tmpnum[0])
			rent_num, _ = strconv.Atoi(tmpnum[1])

		} else if len(tmpnum) == 1 {
			esf_num, _ = strconv.Atoi(tmpnum[0])
		}

		var retStr = ""
		retStr = fmt.Sprintf("%s#%s#%s#%s#%s#%d#%d", fenhang, address, xiaoqu, name, mobile, esf_num, rent_num)
		//s(retStr)
		outlog(retStr)
	})
}

func getPhone(url string) string {
	return ""
}

//获取doc
func getDoc(url string) (*goq.Document, error) {
	res, e := getResp(url)
	if e != nil {
		return nil, e
	}
	doc, e := goq.NewDocumentFromResponse(res)
	if e != nil {
		return nil, e
	}
	return doc, nil
}

//获取response
func getResp(url string) (*http.Response, error) {
	req, e := http.NewRequest("GET", url, nil)
	req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
	//req.Header.Add("Accept-Encoding", "gzip,deflate,sdch")
	req.Header.Add("Accept-Language", "zh-CN,zh;q=0.8")
	req.Header.Add("Cache-Control", "max-age=0")
	req.Header.Add("Proxy-Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
	c := &http.Client{
		Transport: &http.Transport{
			Dial: func(netw, addr string) (net.Conn, error) {
				deadline := time.Now().Add(25 * time.Second)
				c, err := net.DialTimeout(netw, addr, time.Second*20)
				if err != nil {
					return nil, err
				}
				c.SetDeadline(deadline)
				return c, nil
			},
		},
	}

	var resp *http.Response
	//尝试三次
	for retry := 0; retry < 3; retry++ {
		resp, e = c.Do(req)
		if e != nil {
			time.Sleep(time.Second * 1)
		} else {
			break
		}
	}
	if e != nil {
		return nil, e
	}
	return resp, nil
}

//gbk->utf8
func G2U(s string) string {
	ret, e := gbktoutf8([]byte(s))
	if e != nil {
		return ""
	}
	return string(ret)
}

//utf8->gbk
func U2G(s string) string {
	ret, e := utf8togbk([]byte(s))
	if e != nil {
		return ""
	}
	return string(ret)
}

func gbktoutf8(s []byte) ([]byte, error) {
	reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
	d, e := ioutil.ReadAll(reader)
	if e != nil {
		return nil, e
	}
	return d, nil
}
func utf8togbk(s []byte) ([]byte, error) {
	reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
	d, e := ioutil.ReadAll(reader)
	if e != nil {
		return nil, e
	}
	return d, nil
}

func filterStr(str string) string {
	str = strings.Replace(str, "\r\n", "", -1)
	str = strings.Replace(str, "\r", "", -1)
	str = strings.Replace(str, "\n", "", -1)
	str = strings.Replace(str, "#", "", -1)
	str = strings.TrimSpace(str)
	return str
}

func s(a ...interface{}) {
	fmt.Println(a)
}

//outlog
func outlog(str string) {
	pwd, _ := os.Getwd()
	outlog := fmt.Sprintf("%s/%s_log.txt", pwd, LOG)
	file, err := os.OpenFile(outlog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
	defer file.Close()
	if err != nil {
		return
	}
	str += "\n"
	file.WriteString(str)
}
